<template>
  <div>
    <a-form id="formLogin" class="user-layout-login" ref="formLogin" :form="form" @submit="handleSubmit">
      <a-form-item>
        <a-input
          size="large"
          type="text"
          placeholder="请输入手机号"
          v-decorator="['userName', { rules: [{ required: true, message: '请输入手机号' }] }]"
        >
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input>
      </a-form-item>

      <a-form-item>
        <div style="display: flex">
          <div style="flex: 7">
            <a-input
              size="large"
              type="text"
              autocomplete="false"
              placeholder="请输入验证码"
              v-decorator="['SmsCode', { rules: [{ required: false, message: '请输入验证码' }] }]"
            >
              <a-icon slot="prefix" type="woman" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input>
          </div>

          <div style="flex: 3; margin-left: 5px">
            <a-button
              class="button_obtain"
              v-show="sendSmsCode"
              @click.prevent="codeChange"
              style="outline: none; height: 40px"
              type="primary"
            >
              获取验证码
            </a-button>
            <span v-show="!sendSmsCode" class="auth_text">
              <span class="auth_text_blue">{{ auth_time }}</span
              >秒之后重发</span
            >
          </div>
        </div>
      </a-form-item>

      <a-form-item>
        <a-button size="large" type="primary" htmlType="submit" class="login-button">确定</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { loginResult } from './homelogin.js'

const reg = /^1[345789]\d{9}$/ // 手机号正则
export default {
  data() {
    return {
      sendSmsCode: true, //显示‘获取按钮'还是'倒计时'
      auth_time: 0 /*倒计时 计数器*/,
      loading: false,
      customActiveKey: 'tab1',
      form: this.$form.createForm(this),
      required: false,
    }
  },
  mounted() {

  },
  methods: {
    // 发送验证码
    codeChange() {
      const values = this.form.getFieldsValue()
      this.form.validateFields((errors, values) => {
        // 校验成功
        if (!errors) {
          values = this.form.getFieldsValue()
          // 校验手机号格式
          if (!reg.test(values.userName)) return this.$message.error('请核对手机号格式')
          this.loading = true
          this.$http.post('/Base_Manage/Base_User/GenerateVerificationCode?Phone=' + values.userName).then((res) => {
            if (res.Success) {
              this.$message.success('短信发送成功')
              // 显示正在发送
              this.sendSmsCode = false
              //设置倒计时秒
              this.auth_time = 60
              var auth_timetimer = setInterval(() => {
                this.auth_time--
                if (this.auth_time <= 0) {
                  this.sendSmsCode = true
                  clearInterval(auth_timetimer)
                }
              }, 1000)
              this.loading = false
            } else {
              this.loading = false
              this.$message.error(res.Msg)
            }
          })
        }
      })
    },
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((errors, values) => {
        // 校验成功
        if (!errors) {
          values = this.form.getFieldsValue()
          // 校验手机号格式
          if (!reg.test(values.userName)) return this.$message.error('请核对手机号格式')
          // 判断验证码
          if (!values.SmsCode) return this.$message.error('请输入验证码')
          this.loading = true
          this.$http
            .post('/Base_Manage/Home/SubmitLogin', values)
            .then((resJson) => {
              if (resJson.Success) {                
                loginResult(resJson)
              } else {
                this.$message.error(resJson.Msg)
              }
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>

.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>